<!--
 * @Description: 
 * @Author: charles
 * @Date: 2021-11-10 14:11:48
 * @LastEditors: charles
 * @LastEditTime: 2021-11-10 15:53:08
-->
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>受控表单</title>
  <script src="https://cdn.bootcdn.net/ajax/libs/react/17.0.2/umd/react.production.min.js"></script>
  <script src="https://cdn.bootcdn.net/ajax/libs/react-dom/17.0.2/umd/react-dom.production.min.js"></script>
  <script src="https://cdn.bootcss.com/babel-standalone/6.26.0/babel.min.js"></script>
  <script src="https://cdn.bootcdn.net/ajax/libs/lodash.js/4.17.21/lodash.js"></script>
</head>
<body>
  <div id="app"></div>
  <script type="text/babel">

    class App extends React.Component {
      constructor(props){
        super(props)
        // state,method,ref
        this.state = {
          form : {
            name:"默认值",
            age:1,
            gender:"女"
          }
        }
        //this.inputChageHandler = _.debounce(this.inputChageHandler,200);
      }

      submitHandler(event){
        event.preventDefault();
        alert('提交');
      }
      inputChageHandler(event,key){
        let val = event.target.value;
        this.setState({
          form:{
            ...this.state.form,
            [key]:val
          }
        })
      }
     
      render(){
        return (
          <div>
            <h2>受控表单</h2>  
            state : { JSON.stringify(this.state.form) }
            <form action="" onSubmit={()=>{
              this.submitHandler(event)
            }}>
              姓名 <input type="text" onChange={()=>{
                this.inputChageHandler(event,'name')
              }}/>  
              年龄 <input value={this.state.form.age} type="text" onChange={()=>{
                this.inputChageHandler(event,'age')
              }}/>
              性别 <input value={this.state.form.gender} type="text" onChange={()=>{
                this.inputChageHandler(event,'gender')
              }}/>
              <input type="submit" value="提交"/>
            </form>
          </div>
        )
      }
    }
    ReactDOM.render(<App/>,document.getElementById('app'));
  </script>
</body>
</html>
